package com.zhu.pre.controller;

import com.sanploy.common.domain.Cart;
import com.sanploy.common.vo.BaseException;
import com.zhu.pre.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zhu yingzhi on 2017/9/6.
 * @author yingzhi zhu
 * 用户购物车接口 路由类
 */
@RestController
@RequestMapping("/m/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车 接口
     * @param cart 实体类
     * @param token 请求token
     * @param goods_id 商品id
     * @param goods_attr 商品参数
     * @param goods_number  商品数量
     * @return
     */
    @PostMapping("")
    public Object saveCart(Cart cart, @RequestParam String token,@RequestParam Long goods_id,@RequestParam String goods_name,
                           @RequestParam(defaultValue = "") String goods_attr,@RequestParam int goods_number,
                           @RequestParam(defaultValue = "") String goods_attrs_id,@RequestParam double goods_price,
                           @RequestParam String goods_attrs_price) throws BaseException {
        return cartService.addCart(cart,token);
    }

    /**
     * 获取用户的购物车接口
     * @param token 请求token
     * @return
     */
    @GetMapping("")
    public Object getUserCart(@RequestParam String token) throws BaseException {
        return cartService.findUserCart(token);
    }

    /**
     * 购物车商品数量修改
     * @param cart_id 购物车id
     * @param number 购物车数量
     * @return
     */
    @PostMapping("{cart_id}")
    public Object editCartNumber(@PathVariable Long cart_id,@RequestParam String token,@RequestParam int number) throws BaseException {
            return cartService.editCart(cart_id, token, number);
    }

    /**
     * 查找用户购物车里的商品的条数
     * @param token
     * @return
     * @throws BaseException
     */
    @GetMapping("/count")
    public Object getCartCount(@RequestParam String token) throws BaseException {
            return cartService.countCart(token);
    }

    /**
     * 删除商品
     * @param token 请求token
     * @param cart_ids 格式：1,2
     * @return
     * @throws BaseException
     */
    @GetMapping("/delete")
    public Object deleteCart(@RequestParam String token,String[] cart_ids) throws BaseException {
        return cartService.deleteCart(token, cart_ids);
    }


}
